home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap05 / WhatSize / WhatSize.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.1 KB  |  123 lines

  1. /*-----------------------------------------
  2.    WHATSIZE.C -- What Size is the Window?
  3.                  (c) Charles Petzold, 1998
  4.   -----------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  9.  
  10. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  11.                     PSTR szCmdLine, int iCmdShow)
  12. {
  13.      static TCHAR szAppName[] = TEXT ("WhatSize") ;
  14.      HWND         hwnd ;
  15.      MSG          msg ;
  16.      WNDCLASS     wndclass ;
  17.      
  18.      wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  19.      wndclass.lpfnWndProc   = WndProc ;
  20.      wndclass.cbClsExtra    = 0 ;
  21.      wndclass.cbWndExtra    = 0 ;
  22.      wndclass.hInstance     = hInstance ;
  23.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  24.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  25.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  26.      wndclass.lpszMenuName  = NULL ;
  27.      wndclass.lpszClassName = szAppName ;
  28.      
  29.      if (!RegisterClass (&wndclass))
  30.      {
  31.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  32.                       szAppName, MB_ICONERROR) ;
  33.           return 0 ;
  34.      }
  35.      
  36.      hwnd = CreateWindow (szAppName, TEXT ("What Size is the Window?"),
  37.                           WS_OVERLAPPEDWINDOW,
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           NULL, NULL, hInstance, NULL) ;
  41.      
  42.      ShowWindow (hwnd, iCmdShow) ;
  43.      UpdateWindow (hwnd) ;
  44.      
  45.      while (GetMessage (&msg, NULL, 0, 0))
  46.      {
  47.           TranslateMessage (&msg) ;
  48.           DispatchMessage (&msg) ;
  49.      }
  50.      return msg.wParam ;
  51. }
  52.  
  53. void Show (HWND hwnd, HDC hdc, int xText, int yText, int iMapMode,
  54.            TCHAR * szMapMode)
  55. {
  56.      TCHAR szBuffer [60] ;
  57.      RECT  rect ;
  58.      
  59.      SaveDC (hdc) ;
  60.      
  61.      SetMapMode (hdc, iMapMode) ;
  62.      GetClientRect (hwnd, &rect) ;
  63.      DPtoLP (hdc, (PPOINT) &rect, 2) ;
  64.      
  65.      RestoreDC (hdc, -1) ;
  66.      
  67.      TextOut (hdc, xText, yText, szBuffer,
  68.               wsprintf (szBuffer, TEXT ("%-20s %7d %7d %7d %7d"), szMapMode,
  69.               rect.left, rect.right, rect.top, rect.bottom)) ;
  70. }  
  71.  
  72. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  73. {
  74.      static TCHAR szHeading [] =
  75.           TEXT ("Mapping Mode            Left   Right     Top  Bottom") ;
  76.      static TCHAR szUndLine [] = 
  77.           TEXT ("------------            ----   -----     ---  ------") ;
  78.      static int   cxChar, cyChar ;
  79.      HDC          hdc ;
  80.      PAINTSTRUCT  ps ;
  81.      TEXTMETRIC   tm ;
  82.      
  83.      switch (message)
  84.      {
  85.      case WM_CREATE:
  86.           hdc = GetDC (hwnd) ;
  87.           SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  88.           
  89.           GetTextMetrics (hdc, &tm) ;
  90.           cxChar = tm.tmAveCharWidth ;
  91.           cyChar = tm.tmHeight + tm.tmExternalLeading ;
  92.           
  93.           ReleaseDC (hwnd, hdc) ;
  94.           return 0 ;
  95.           
  96.      case WM_PAINT:
  97.           hdc = BeginPaint (hwnd, &ps) ;
  98.           SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  99.           
  100.           SetMapMode (hdc, MM_ANISOTROPIC) ;
  101.           SetWindowExtEx (hdc, 1, 1, NULL) ;
  102.           SetViewportExtEx (hdc, cxChar, cyChar, NULL) ;
  103.           
  104.           TextOut (hdc, 1, 1, szHeading, lstrlen (szHeading)) ;
  105.           TextOut (hdc, 1, 2, szUndLine, lstrlen (szUndLine)) ;
  106.           
  107.           Show (hwnd, hdc, 1, 3, MM_TEXT,      TEXT ("TEXT (pixels)")) ;
  108.           Show (hwnd, hdc, 1, 4, MM_LOMETRIC,  TEXT ("LOMETRIC (.1 mm)")) ;
  109.           Show (hwnd, hdc, 1, 5, MM_HIMETRIC,  TEXT ("HIMETRIC (.01 mm)")) ;
  110.           Show (hwnd, hdc, 1, 6, MM_LOENGLISH, TEXT ("LOENGLISH (.01 in)")) ;
  111.           Show (hwnd, hdc, 1, 7, MM_HIENGLISH, TEXT ("HIENGLISH (.001 in)")) ;
  112.           Show (hwnd, hdc, 1, 8, MM_TWIPS,     TEXT ("TWIPS (1/1440 in)")) ;
  113.           
  114.           EndPaint (hwnd, &ps) ;
  115.           return 0 ;
  116.           
  117.      case WM_DESTROY:
  118.           PostQuitMessage (0) ;
  119.           return 0 ;
  120.      }
  121.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  122. }
  123.